<?php
/**
 * 后台-内容控制器
 * ContentController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author: huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class ContentController extends AdminController
{   
    protected $oModel;
    protected $oPageModel;
    protected $_attributeLabels;
    protected $_attributePageLabels;
	public function __construct($id, $module) {
	    //模型
		$this->oModel = new AdminCategory();
        $this->_attributeLabels = $this->oModel->attributeLabels();
        //单页数据模型
        $this->oPageModel = new CmsPageModel();
        $this->_attributePageLabels = $this->oPageModel->attributeLabels();
		return parent::__construct($id, $module);
	}
    //分类管理
	public function actionIndex(){
	    $iCateId = $this->_request->getQuery('cate_id' ,0);
        if( $iCateId ){
            //根据栏目获取模型ID
            $oCategoryModel = CategoryModel::model()->findByPk($iCateId);
            $iModelId = $oCategoryModel->model_id;
            //获取模型数据
            $oModel = CmsModel::model()->findByPk($iModelId);
            //获取模型表所有字段
            $oModelField = CmsModelField::model()->findAll('model_id='.$iModelId);
            //根据模型获取对应表
            $sTableName = Yii::app()->db->tablePrefix .'cms_'. $oModel->table_name;
            $where = "WHERE catid=".$iCateId;
            $sSQL = "SELECT * FROM $sTableName $where ORDER BY sort ASC ,id DESC ,inputtime DESC";
            //分页
            $criteria = new CDbCriteria();
            $oResult = Yii::app()->db->createCommand($sSQL)->query();
            $pages = new CPagination($oResult->rowCount);
            $pages->pageSize = $this->pagesize;
            $pages->applyLimit($criteria);
            $oResult = Yii::app()->db->createCommand($sSQL." LIMIT :offset,:limit");
            $oResult->bindValue(':offset' ,$pages->currentPage*$pages->pageSize);
            $oResult->bindValue(':limit' ,$pages->pageSize);
            $aResult = $oResult->query();
            $this->render('list' ,array('aResult'=>$aResult ,'pages'=>$pages ,'iCateId'=>$iCateId));
        } else {
            $jCategory = AdminCategory::getCategoryJson(0);
		    $this->render('index' ,array('jCategory'=>$jCategory));
        }
	    
	}
    /**
     * ContentController::actionAdd()
     * 
     * @return void
     */
    public function actionAdd(){
        $iCateId = $this->_request->getQuery('cate_id' ,0);
        //echo $iCateId;exit;
        //调用内容类生成对应表单数组
        $oCategoryModel = CategoryModel::model()->findByPk($iCateId);
        $iModelId = $oCategoryModel->model_id;
        
        $oCategory = new CategoryModel();
        
        $oContentForm = new ContentForm($iModelId,$iCateId,$oCategory->getAllCategory());
        
        $aFormInfo = $oContentForm->get();
        //echo '<pre>';
        //print_r($forminfos);exit;
        if( isset($_POST) && $_POST ){
            //获取模型数据
            $oModel = CmsModel::model()->findByPk($iModelId);
            //根据模型获取对应表
            $sTableName = Yii::app()->db->tablePrefix .'cms_'. $oModel->table_name;
            $aQueryData = $_POST['info'];
            $aQueryData['inputtime'] = strtotime($aQueryData['inputtime']);
            $aQueryData['updatetime'] = strtotime($aQueryData['inputtime']);
            $aQueryData['posids'] = join(',' ,$aQueryData['posids']);
            
            //print_r($aQueryData);exit;
            Yii::app()->db->createCommand()->insert($sTableName ,$aQueryData);
            //修改URL
            if( $aQueryData['islink']==1 && (isset($_POST['linkurl']) && $_POST['linkurl']) ){
                $url = $_POST['linkurl'];
            } else {
                $url = $this->createUrl('/content/show' ,array('catid'=>$aQueryData['catid'] ,'id'=>Yii::app()->db->getLastInsertID()));
            }            
            Yii::app()->db->createCommand()->update($sTableName ,array('url'=>$url) ,'id=:id' ,array(':id'=>Yii::app()->db->getLastInsertID()));
            //成功后更新栏目数据
            AdminCategory::updateCounts(1 ,$iCateId);
            //更新附件状态
            if( isset($_POST['aid']) && count($_POST['aid'])>0 ){
                AdminAttachment::updateData(array('isuse'=>1) ,'aid in('.join(',' ,$_POST['aid']).')');
            }
            $this->showMessage(0 ,'添加内容成功' ,$this->createUrl('content/index' ,array('cate_id'=>$aQueryData['catid'])));
            
        }
        $this->render('_form' ,array('forminfo'=>$aFormInfo,'iCateId'=>$iCateId));
    }
    /**
     * ContentController::actionEdit()
     * 
     * @return void
     */
    public function actionEdit(){
        $iCateId = $this->_request->getQuery('cate_id' ,0);
        $id = $this->_request->getQuery('id' ,0);
        //echo $iCateId;exit;
        //调用内容类生成对应表单数组
        $oCategoryModel = CategoryModel::model()->findByPk($iCateId);
        $iModelId = $oCategoryModel->model_id;
        //获取模型数据
        $oModel = CmsModel::model()->findByPk($iModelId);
        //根据模型获取对应表
        $sTableName = Yii::app()->db->tablePrefix .'cms_'. $oModel->table_name;
        $where = 'WHERE id='.$id;
        $sSQL = "SELECT * FROM $sTableName $where ";
        $oResult = Yii::app()->db->createCommand($sSQL)->queryRow(true);
        //print_r($oResult);exit;
        $oCategory = new CategoryModel();
        
        $oContentForm = new ContentForm($iModelId,$iCateId,$oCategory->getAllCategory());
        
        $aFormInfo = $oContentForm->get($oResult);
        //echo '<pre>';
        //print_r($forminfos);exit;
        if( isset($_POST) && $_POST ){
            
            $aQueryData = $_POST['info'];
            $aQueryData['inputtime'] = strtotime($aQueryData['inputtime']);
            $aQueryData['updatetime'] = time();
            $aQueryData['posids'] = join(',' ,$aQueryData['posids']);
            
            //修改URL
            if( $aQueryData['islink']==1 && (isset($_POST['linkurl']) && $_POST['linkurl']) ){
                $aQueryData['url'] = $_POST['linkurl'];
            } else {
                $aQueryData['url'] = $this->createUrl('/content/show' ,array('catid'=>$aQueryData['catid'] ,'id'=>$id));
            }            
            Yii::app()->db->createCommand()->update($sTableName ,$aQueryData ,'id=:id' ,array(':id'=>$id));
            //更新附件状态
            if( isset($_POST['aid']) && count($_POST['aid'])>0 ){
                AdminAttachment::updateData(array('isuse'=>1) ,'aid in('.join(',' ,$_POST['aid']).')');
            }
            $this->showMessage(0 ,'添加内容成功' ,$this->createUrl('content/index' ,array('cate_id'=>$aQueryData['catid'])));
            
        }
        $this->render('_form' ,array('forminfo'=>$aFormInfo ,'iCateId'=>$iCateId));
    }
    /**
     * ContentController::actionaddpage() 添加间页栏目内容
     * 
     * @return void
     */
    public function actionaddpage(){
        $iCateId = $this->_request->getQuery('cate_id' ,0);
        $oPageModel = $this->oPageModel->findByPk($iCateId);
        if($oPageModel) $this->oPageModel = $oPageModel;
        if( isset($_POST) && $_POST ){
            $aData = $_POST['CmsPageModel'];
            $aData['content'] = $_POST['content'];
            $aData['updatetime'] = time();
            $this->oPageModel->attributes = $aData;
            
            if( $this->oPageModel->save() ){
                $this->showMessage(0 ,'编辑内容成功' ,$this->createUrl('content/addpage' ,array('cate_id'=>$iCateId)));
            } else {
                $this->showMessage(1 ,'编辑内容失败，请检查页面内容是否有错' ,$this->createUrl('content/addpage' ,array('cate_id'=>$iCateId)));
            }
            
        }
        $this->render('_formpage' ,array('iCateId'=>$iCateId));
    }
    public function actionDelete(){
        $iCateId = $this->_request->getQuery('cate_id' ,0);
        $id = $this->_request->getQuery('id' ,0);
        //echo $iCateId;exit;
        //调用内容类生成对应表单数组
        $oCategoryModel = CategoryModel::model()->findByPk($iCateId);
        $iModelId = $oCategoryModel->model_id;
        //获取模型数据
        $oModel = CmsModel::model()->findByPk($iModelId);
        //根据模型获取对应表
        $sTableName = Yii::app()->db->tablePrefix .'cms_'. $oModel->table_name;
        $where = 'WHERE id='.$id;
        $sSQL = "DELETE FROM $sTableName $where ";
        if( Yii::app()->db->createCommand($sSQL)->execute() ){
            AdminCategory::updateCounts(-1 ,$iCateId);
            $this->ajaxReturn(0 ,'删除成功');
        } else {
            $this->ajaxReturn(1 ,'删除失败');
        }
    }
    //批量删除用户
	public function actionDeleteall(){
	    $iCateId = $this->_request->getQuery('cate_id' ,0);
        //$id = $this->_request->getQuery('id' ,0);
        //echo $iCateId;exit;
        //调用内容类生成对应表单数组
        $oCategoryModel = CategoryModel::model()->findByPk($iCateId);
        $iModelId = $oCategoryModel->model_id;
        //获取模型数据
        $oModel = CmsModel::model()->findByPk($iModelId);
        //根据模型获取对应表
        $sTableName = Yii::app()->db->tablePrefix .'cms_'. $oModel->table_name;
        
		$check=1;
		$ids=explode('|',$_POST['ids']);
		foreach ((array)$ids as $v){
		    $where = 'WHERE id='.$v;
            $sSQL = "DELETE FROM $sTableName $where ";
            if( Yii::app()->db->createCommand($sSQL)->execute() ){
                AdminCategory::updateCounts(-1 ,$iCateId);
            }else{
				$check=0;
			}
		}
		if($check){
			$this->ajaxReturn(0,'删除成功');
		}else{
			$this->ajaxReturn(1,'删除失败');
		}
	}
}
